package com.tool.mylibrary.ui.popupWindow;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.tool.mylibrary.R;

import java.util.List;

/**
 * Created by Google on 2018/6/7.
 */

/*
 * 使用方式
 *  DropWindow.init(DialogActivity.this)
                        .setData(strings)
                        .setHeight(1,5)
                        .setOnSetShowListener(new DropWindow.OnSetShowListener() {
                            @Override
                            public void onView(TextView textView, int position) {
                                textView.setText(strings.get(position));
                            }

                            @Override
                            public void onClick(PopupWindow popupWindow, int position) {
                                show_popupwindow.setText(strings.get(position));
                                Toast.makeText(DialogActivity.this,"杜兰特sb",Toast.LENGTH_SHORT).show();
                                popupWindow.dismiss();
                            }
                        }).showDropDown(btn1);
 */
//注意，一定要先设置数据和监听，再显示
public class DropWindow {

    private ListView listView;
    private PopupShowAdapter adapter;
    private List<?> data;
    private int height; //高度
    private Context context;
    private int offsetY;//Y轴偏移量，可以不用设置
    public static DropWindow init(Context context) {
        return new DropWindow(context);
    }

    public  DropWindow(Context context){
        this.context=context;
    }
    private String bgColor = "#ffffff";

    public DropWindow setBgColor(String bgColor) {
        this.bgColor = bgColor;
        return this;
    }
    //设置数据
    public DropWindow setData(List<?> data) {
        this.data = data;
        return this;
    }
    //设置高度
    public DropWindow setHeight(int height){
        this.height=height;
        return this;
    }
    //设置偏移量--像素
    public DropWindow setOffsetY(int y){
        this.offsetY=y;
        return this;
    }

    /*  屏幕的几分之几
        first 分子，second 分母
     */
    public DropWindow setHeight(int first,int second){
        if (first<second&&second!=0){
            this.height=getScreenHeight(context)*first/second;
        }
        return this;
    }

    //最后一步调用-展示出来
    public DropWindow showDropDown(View bootView) {
        View contentView = LayoutInflater.from(context).inflate(R.layout.pop_dropdown_view, null);
        listView = contentView.findViewById(R.id.list_view_p);
        listView.setBackgroundColor(Color.parseColor(bgColor));
        adapter = new PopupShowAdapter(context, data);
        adapter.setOnSetView(new PopupShowAdapter.OnSetView() {
            @Override
            public void setView(TextView view, int position) {
                if (onSetShowListener != null) {
                    onSetShowListener.onView(view, position);
                }
            }
        });
        listView.setAdapter(adapter);

        if (height==0){
            height=ViewGroup.LayoutParams.WRAP_CONTENT;
        }

        final PopupWindow popupWindow = new PopupWindow(contentView,
                bootView.getWidth(),
                height);

        ColorDrawable cd = new ColorDrawable(context.getResources().getColor(R.color.transparent));
        popupWindow.setBackgroundDrawable(cd);
        popupWindow.setAnimationStyle(R.style.PopupAnimation);
        popupWindow.update();
        popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        popupWindow.setTouchable(true); // 设置popupwindow可点击
        popupWindow.setOutsideTouchable(true); // 设置popupwindow外部可点击
        popupWindow.setFocusable(true); // 获取焦点
        // 设置popupwindow的位置（相对tvLeft的位置）
        int topBarHeight = bootView.getBottom();
        if (offsetY==0){
            offsetY=(bootView.getHeight()) / 3;
        }else if (offsetY>0&&offsetY<10){//如果设置的是10以内的像素，就是当前控件的几分之几
            offsetY=(bootView.getHeight())/offsetY;
        }
        popupWindow.showAsDropDown(bootView, 0,offsetY);


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (onSetShowListener != null) {
                    onSetShowListener.onClick(popupWindow, position);
                }

            }
        });
        popupWindow.setTouchInterceptor(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // 如果点击了popupwindow的外部，popupwindow也会消失
                if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                    popupWindow.dismiss();
                    return true;
                }
                return false;
            }
        });

        return this;
    }


    public interface OnSetShowListener {
        void onView(TextView textView, int position);

        void onClick(PopupWindow popupWindow, int position);
    }

    OnSetShowListener onSetShowListener;

    //一定要在展示之前调用
    public DropWindow setOnSetShowListener(OnSetShowListener onSetShowListener) {
        this.onSetShowListener = onSetShowListener;
        return this;
    }

    //获取屏幕高度 px
    private  int getScreenHeight(Context context){
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        return displayMetrics.heightPixels;
    }
}
